package org.jiaxin.nutrient.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author: jiaxin
 * @Description : TODO 整理需求中的一些 小功能的实现
 * @date: 2023/1/18 15:46
 **/

public class CommonUtils {

    /**
     * TODO 下划线转驼峰的方法
     * @param s
     * @return
     */
    public static String underlineToCamel(String s){
        if (StringUtils.isBlank(s)){
            throw new IllegalArgumentException("输入的字符串不为空 str="+s);
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '_') {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }


    /**
     * 统计 十进制转为2进制数字之后1的个数
     * @param n
     * @return
     */
    public static int bit1Count(long n){
        return n == 0 ? 0 : 1 + bit1Count(n & (n - 1));
    }

    /**
     * 查询某个文件夹 内所有的文件
     * @param file
     * @return
     */
    public static List<String> findFile(File file) {
        File[] fs = file.listFiles();
        List<String> list = new LinkedList<>();
        if (fs != null) {
            for(File f: fs){
               /* if (f.isDirectory()) {
                    // 文件夹 递归
                    findFile(f);
                } else */
                if (f.isFile()) {
                    //System.out.println(f);
                    list.add(f.toString());
                }
            }
        }
        return list;
    }
    //驼峰转下划线
    public static String camelToUnderLine(String camelCase) {
        StringBuilder snakeCase = new StringBuilder();
        char[] chars = camelCase.toCharArray();

        for (char currentChar : chars) {
            if (Character.isUpperCase(currentChar)) {
                snakeCase.append('_');
                snakeCase.append(Character.toLowerCase(currentChar));
            } else {
                snakeCase.append(currentChar);
            }
        }

        return snakeCase.toString();
    }


    public static Map<String,Object>returnMap(String message,Integer status){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("message",message);
        resultMap.put("status",status);
        return resultMap;
    }

    public static Map<String,Object> returnMap(Object data,String message,Integer status){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("data",data);
        resultMap.put("message",message);
        resultMap.put("status",status);
        return resultMap;
    }




    public static String getOrDefault(String o,String defaultValue){
        if (StringUtils.isBlank(o)){
            return defaultValue;
        }

        return o;
    }

    public static double Keep2DecimalPlaces(double number){

        DecimalFormat df = new DecimalFormat("#.##");
        df.setRoundingMode(RoundingMode.HALF_UP);



        return Double.parseDouble(df.format(number));

    }
}
